#!/bin/ksh

get_prop(){
        propfile=$1
        key=$2
        #echo `grep $2 $1 | cut -d"=" -f2`
	echo `sed '/^\#/d' $1 | grep $key  | tail -n 1 | cut -d "=" -f2-`
}

SCRIPT_ROOT=$(cd `dirname ${0}`; pwd)

###############################################################################
# LOADING PROPERTIS FROM FILE
###############################################################################
get_prop $SCRIPT_ROOT/wbiBot.properties wbi.istance.name|read WBI_NAME
get_prop $SCRIPT_ROOT/wbiBot.properties wbi.istance.admin_user|read WBI_USER
get_prop $SCRIPT_ROOT/wbiBot.properties wbi.script.dir|read WBI_HOME
get_prop $SCRIPT_ROOT/wbiBot.properties mq.istance.name|read MQ_NAME

get_prop $SCRIPT_ROOT/wbiBot.properties message.ics.notrunning|read MESSAGE_ICS_NOT_RUNNING
get_prop $SCRIPT_ROOT/wbiBot.properties message.mq.notrunning|read MESSAGE_MQ_NOT_RUNNING
get_prop $SCRIPT_ROOT/wbiBot.properties message.ca.notrunning|read MESSAGE_CA_NOT_RUNNING
get_prop $SCRIPT_ROOT/wbiBot.properties message.wbi.env|read MESSAGE_WBI_ENV
get_prop $SCRIPT_ROOT/wbiBot.properties message.ics.partizione|read MESSAGE_WBI_PARTIZIONE
get_prop $SCRIPT_ROOT/wbiBot.properties message.ics.startcommand|read MESSAGE_ICS_STARTCOMMAND
get_prop $SCRIPT_ROOT/wbiBot.properties message.mq.startcommand|read MESSAGE_MQ_STARTCOMMAND
get_prop $SCRIPT_ROOT/wbiBot.properties message.ca.startcommand|read MESSAGE_CA_STARTCOMMAND

get_prop $SCRIPT_ROOT/wbiBot.properties mail.activated|read MAIL_STATUS
get_prop $SCRIPT_ROOT/wbiBot.properties mail.subject|read MAIL_SUBJECT

# SEMAFORO
get_prop $SCRIPT_ROOT/wbiBot.lock wbiBot.lock.value|read SEMAFORO

NUM_AGENT=`grep -v ^# ./elenco_connettori.list|wc -l`
NUM_AGENT_RUNNING=`ps -ef|grep ConnectorAgent|grep -v grep|wc -l`
ICS=`ps -ef|grep InterchangeSystem|grep -v grep|wc -l`
MQ=`dspmq|grep ${MQ_NAME}|grep -v grep|grep Running|wc -l`
THIS_SERVER=$(hostname)
LOCKFILE=$SCRIPT_ROOT/wbiBot.lock
MESSAGE=""
RC=0

#CONTROLLO CONNETTORI
if [ $NUM_AGENT_RUNNING -ne $NUM_AGENT ]
then
	MESSAGE=$MESSAGE$MESSAGE_CA_NOT_RUNNING"\n"
	MESSAGE=$MESSAGE$MESSAGE_WBI_PARTIZIONE" "$THIS_SERVER"\n"
	MESSAGE=$MESSAGE"sudo su - openroot\n"
	MESSAGE=$MESSAGE"cd "$WBI_HOME"\n"
	MESSAGE=$MESSAGE$MESSAGE_WBI_ENV"\n"
	MESSAGE=$MESSAGE"${MESSAGE_CA_STARTCOMMAND} ALL ${WBI_USER}\n\n"
        RC=1
fi

#CONTROLLO ICS
if [ $ICS -ne 1 ]
then
	MESSAGE=$MESSAGE$MESSAGE_ICS_NOT_RUNNING"\n"
	MESSAGE=$MESSAGE$MESSAGE_WBI_PARTIZIONE" "$THIS_SERVER"\n"
	MESSAGE=$MESSAGE"sudo su - openroot\n"
	MESSAGE=$MESSAGE"cd "$WBI_HOME"\n"
	MESSAGE=$MESSAGE$MESSAGE_WBI_ENV"\n"
	MESSAGE=$MESSAGE"${MESSAGE_ICS_STARTCOMMAND} ${WBI_USER}\n\n"
        RC=1
fi

#CONTROLLO MQ
if [ $MQ -ne 1 ]
then
        MESSAGE=$MESSAGE$MESSAGE_MQ_NOT_RUNNING"\n"
	MESSAGE=$MESSAGE$MESSAGE_WBI_PARTIZIONE" "$THIS_SERVER"\n"
	MESSAGE=$MESSAGE"sudo su - openroot\n"
	MESSAGE=$MESSAGE"cd "$WBI_HOME"\n"
	MESSAGE=$MESSAGE$MESSAGE_WBI_ENV"\n"
	MESSAGE=$MESSAGE"${MESSAGE_MQ_STARTCOMMAND}\n\n"
        RC=1
fi

if [ $RC -ne 0 ]
then
	echo "STATO WBI: ${MESSAGE}"
else
	echo "WBI is up & running"
fi

if [ $RC -ne 0 ]
then
	if [ $SEMAFORO -ne 1 ]
	then
		if [ $MAIL_STATUS -ne 1 ]
		then
			echo $MESSAGE >> $SCRIPT_ROOT/wbiBotMail.txt
	        	mail -s "${MAIL_SUBJECT} ${THIS_SERVER}" marco.gomiero@generaligroup.com < $SCRIPT_ROOT/wbiBotMail.txt
			rm $SCRIPT_ROOT/wbiBotMail.txt
			echo "wbiBot.lock.value=1" > $LOCKFILE
		fi
	fi
else
	echo "" > $LOCKFILE
	echo "wbiBot.lock.value=0" > $LOCKFILE
fi

